#include "../pch.h"
#include "REFXMLDocument.h"
#include "core/REFNameTable.h"

namespace BFX
{

REFXMLEntity::REFXMLEntity(const UniString& strName, const UniString& strData, const UniString& strPublicId,
	const UniString& strSystemId, const UniString& strNotationName, REFXMLDocument* pDoc)
	: REFXMLNode(pDoc)
{
	m_strName		= pDoc->GetNameTable()->Add(strName);
	m_strPublicId	= strPublicId;
	m_strSystemId	= strSystemId;
	m_strNotationName	= strNotationName;
	m_strUnparsedReplacementStr	= strData;

	m_pLastNode	= NULL;
	m_bChildrenFoliating	= false;
}
REFXMLEntity::~REFXMLEntity()
{
}

// Gets the name of the node.
UniString REFXMLEntity::GetName() const
{
	return m_strName;
}
// Gets the name of the node without the namespace prefix.
UniString REFXMLEntity::GetLocalName() const
{
	return m_strName;
}

// Gets the concatenated values of the entity node and all its children.
UniString REFXMLEntity::GetInnerText() const
{
	return REFXMLNode::GetInnerText();
}
// The property is read-only and when tried to be set, exception will be thrown.
void REFXMLEntity::SetInnerText(const UniString& str)
{
	BFX_ENSURE1(false, "The 'InnerText' of an 'Entity' node is read-only and cannot be set.");
}

// Gets the type of the node.
XMLNodeType REFXMLEntity::GetNodeType() const
{
	return XMLNodeType_Entity;
}

bool REFXMLEntity::IsContainer() const
{
	return true;
}

//Without override these two functions, we can't guarantee that WriteTo()/WriteContent() functions will never be called
UniString REFXMLEntity::GetOuterXml() const
{
	return UniString();	// empty
}

UniString REFXMLEntity::GetInnerXml() const
{
	return UniString();	// empty
}
void REFXMLEntity::SetInnerXml(const UniString& str)
{
	BFX_ENSURE1(false, "Cannot set the 'InnerXml' for the current node because it is either read-only or cannot have children.");
}

// Saves the node to the specified XmlWriter.
void REFXMLEntity::WriteTo(REFXMLWriter* w) const
{
	// NOTE: intentionally do nothing
}
// Saves all the children of the node to the specified XmlWriter.
void REFXMLEntity::WriteContentTo(REFXMLWriter* w) const
{
	// NOTE: intentionally do nothing
}

UniString REFXMLEntity::GetBaseURI() const
{
	return m_strBaseURI;
}

// Gets the value of the public identifier on the entity declaration.
UniString REFXMLEntity::GetPublicId() const
{
	return m_strPublicId;
}
// Gets the value of the system identifier on the entity declaration.
UniString REFXMLEntity::GetSystemId() const
{
	return m_strSystemId;
}

// Gets the name of the optional NDATA attribute on the entity declaration.
UniString REFXMLEntity::GetNotationName() const
{
	return m_strNotationName;
}

REFXMLLinkedNode* REFXMLEntity::GetFirstNode() const
{
	if (m_pFirstNode == NULL && !m_bChildrenFoliating)
	{
		// expand the unparsedreplacementstring
		m_bChildrenFoliating = true;
		// wrap the replacement string with an element
		// TODO: xxx
		BFX_ASSERT(false);
	}
	return m_pFirstNode;
}
void REFXMLEntity::SetFirstNode(REFXMLLinkedNode* pFirstNode)
{
	m_pFirstNode = pFirstNode;
}

REFXMLLinkedNode* REFXMLEntity::GetLastNode() const
{
	if (m_pLastNode == NULL && !m_bChildrenFoliating)
	{
		// expand the unparsedreplacementstring
		m_bChildrenFoliating = true;
		// wrap the replacement string with an element
		// TODO: xxx
		BFX_ASSERT(false);
	}
	return m_pLastNode;
}
void REFXMLEntity::SetLastNode(REFXMLLinkedNode* pLastNode)
{
	m_pLastNode = pLastNode;
}

bool REFXMLEntity::IsValidChildType(XMLNodeType type) const
{
	return(type == XMLNodeType_Text ||
		type == XMLNodeType_Element ||
		type == XMLNodeType_ProcessingInstruction ||
		type == XMLNodeType_Comment ||
		type == XMLNodeType_CDATA ||
		type == XMLNodeType_Whitespace ||
		type == XMLNodeType_SignificantWhitespace ||
		type == XMLNodeType_EntityReference);
}

void REFXMLEntity::SetBaseURI(const UniString& strBaseURI)
{
	m_strBaseURI = strBaseURI;
}

}	//	namespace BFX
